﻿using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Navigation;
using Samples.Radio.Agent;
using Samples.Radio.Data;

namespace Samples.Radio.Pages.Settings
{
    public class SettingsPageViewModel:ViewModelBase
    {
        private readonly ObservableCollection<TrackViewModel> audioStreams = new ObservableCollection<TrackViewModel>();
        public ObservableCollection<TrackViewModel> AudioStreams { get { return audioStreams; }}

        private TrackViewModel currentStream;

        public TrackViewModel CurrentStream
        {
            get { return currentStream; }
            set
            {
                if (CurrentStream == value && value!=null) return;
                currentStream = value;
                RaisePropertyChanged(() => CurrentStream);
            }
        }
        

        //private TrackViewModel _CurrentStream;
        //public TrackViewModel CurrentStream
        //{
        //    get
        //    {
        //        return _CurrentStream;
        //    }
        //    set
        //    {
        //        if (_CurrentPlaybackMode == PlaybackMode.Streaming
        //            && _CurrentStream != null
        //            && _CurrentStream.TrackName != value.TrackName)
        //        {
        //            if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing)
        //            {
        //                //Need to reset the background audio player
        //                BackgroundAudioPlayer.Instance.Stop();
        //                BackgroundAudioPlayer.Instance.Close();
        //            }
        //        }

        //        _CurrentStream = value;
        //        NotifyPropertyChanged("CurrentStream");
        //    }
        //}


        public SettingsPageViewModel(RadioRepository repository, IDispatcher dispatcher, INavigation navigation)
            : base(repository, dispatcher, navigation)
        {
        }

        public override void OnNavigatedTo(NavigationEventArgs navigationEventArgs, NavigationContext context, System.Collections.Generic.IDictionary<string, object> pageState)
        {
            base.OnNavigatedTo(navigationEventArgs, context, pageState);

            if (navigationEventArgs.NavigationMode == NavigationMode.New)
            {
                AudioStreams.Fill(Repository.Configuration.Streams.Select(c => new TrackViewModel
                                                                                   {
                                                                                       TrackName = c.Title,
                                                                                       AlbumArt = c.ImageUri,
                                                                                       Album = c.StreamUri
                                                                                   }).ToList());

                var current = Repository.CurrentStream;
                CurrentStream = AudioStreams.First(tvm => tvm.TrackName == current.Title);
            }

        }

        public override void OnNavigatedFrom(NavigationEventArgs navigationEventArgs, System.Collections.Generic.IDictionary<string, object> pageState)
        {
            base.OnNavigatedFrom(navigationEventArgs, pageState);

            if (CurrentStream != null)
            {
                Repository.CurrentStream =
                    Repository.Configuration.Streams.First(s => s.Title == CurrentStream.TrackName);
            }
        }
    }
}
